package com.example.screenshotlistener

import android.content.ClipData
import android.content.ClipboardManager
import android.content.ClipboardManager.OnPrimaryClipChangedListener
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.text.SimpleDateFormat
import java.util.*

class MainActivity : AppCompatActivity() {

    private lateinit var textView: TextView
    private lateinit var inputView: EditText
    private lateinit var button: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById(R.id.hello_text)
        inputView = findViewById(R.id.input_value)
        button = findViewById(R.id.button)

        /* 剪切板监控代码 START*/
        val cb:ClipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val listener = OnPrimaryClipChangedListener {
            cb.primaryClip.apply {
                this?.apply {
                    textView.text = this.getItemAt(0).text
                }
            }
        }
        cb.addPrimaryClipChangedListener(listener)
        /* 剪切板监控代码 END*/

        // 将输入框的内容复制到剪切板
        button.setOnClickListener {
            cb.setPrimaryClip(ClipData.newPlainText("hello", inputView.text.toString()))
        }
    }

    override fun onResume() {
        super.onResume()
        //  设置回调函数
        FileObserverUtils.setSnapShotCallBack(SnapShotTakeCallBack(this));
        FileObserverUtils.startSnapshotWatching();
    }

    override fun onPause() {
        super.onPause()
        FileObserverUtils.stopSnapshotWatching();
    }
}